package com.liqw.comment.controller;

import com.liqw.comment.entity.Comment;
import com.liqw.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;
    @GetMapping("/list")
    public String findAll(Model model){
        List<Comment> all = commentService.findAll();
        model.addAttribute("all",all);
        return "index";
    }
    @PostMapping("/insert")
    public String insert(Model model, Comment comment){
        //这里模拟页面信息
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        comment.setCreatedatetime(sdf.format(date));
        comment.setArticleid("119");
        comment.setState(1);
        comment.setLikenum(1);
        comment.setNickname("lijin");
        comment.setReplynum(1);
        commentService.insert(comment);
        return "redirect:/list";
    }
    @GetMapping ("/remove")
    public String remove(Model model, String id){
        commentService.remove(id);
        return "redirect:/list";
    }
}